gbDebug = True;

try:
  import xbmcgui;
except:
  # For command line testing outside of XBMC
  from FakeXBMCGUI import FakeXBMCGUI;
  xbmcgui = FakeXBMCGUI();

def getSourceDrivePathAndDeviceName(oSettings):
  from queryDosDevice import queryDosDevice;
  from getDriveType import getDriveType;
  dOpticalDevice_sDeviceName_by_sDrivePath = {};
  dOpticalDevice_sDriveType_by_sDrivePath = {};
  asValidDrivePaths = [];
  for sDriveLetter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
    sDrivePath = sDriveLetter + ":\\";
    sDriveType = getDriveType(sDrivePath);
    if sDriveType not in ["REMOVABLE", "CDROM"]:
      print "%s: Ignoring %s: type = %s" % (__file__, sDrivePath, sDriveType);
    else:
      sDeviceName = queryDosDevice(sDrivePath);
      if not sDeviceName: # and sDeviceName.startswith("\\Device\\CdRom"): => Unfortunately, this is not always true :(
        print "%s: Ignoring %s (%s): no device name" % (__file__, sDriveType, sDrivePath);
      else:
        print "%s: Detected %s (%s) = %s" % (__file__, sDrivePath, sDriveType, sDeviceName);
        asValidDrivePaths.append(sDrivePath);
        dOpticalDevice_sDeviceName_by_sDrivePath[sDrivePath] = sDeviceName;
  if len(asValidDrivePaths) == 1:
    sDrivePath = asValidDrivePaths[0];
    if gbDebug:
      print "%s: Automatically selected %s (%s)" % (__file__, dOpticalDevice_sDeviceName_by_sDrivePath[sDrivePath], sDrivePath);
  else:
    oDialog = xbmcgui.Dialog();
    if not asValidDrivePaths:
      oDialog.ok(oSettings.sName, "No optical drives were found on your system.");
      return None, None;
    iDrivePathIndex = oDialog.select("Please select the optical drive to use.", asValidDrivePaths);
    sDrivePath = asValidDrivePaths[iDrivePathIndex];
    if gbDebug:
      print "%s: User selected %s (%s)" % (__file__, dOpticalDevice_sDeviceName_by_sDrivePath[sDrivePath], sDrivePath);
  return sDrivePath, dOpticalDevice_sDeviceName_by_sDrivePath[sDrivePath];
